import i18n from '@root/i18n';

// 当前i18n配置已支持语言自动切换，客户端下默认会跟随用户浏览器语言设置。例如，若用户浏览器语言为 zh-CN，则界面将显示为简体中文。
console.log('(client)：', i18n.t('welcome_game'));
console.log('(client)：', i18n.t('welcome_ap'));
console.log(
  '(client)：',
  i18n.t('navigator.language', { language: navigator.language })
);

import { dialog } from './dialog';

import type { languageData, dialogData } from '../../shares/sharesApp';
import { remoteType } from '../../shares/sharesApp';
import { geti18n } from './geti18n';

remoteChannel.events.on('client', (event: dialogData) => {
  const { type, data } = event;
  switch (type) {
    case 'dialog':
      if (!dialog.backGround.visible) {
        const text = geti18n(data);
        if (Array.isArray(text)) {
          // 顺序逐个弹窗
          (async () => {
            for (const t of text) {
              await dialog.show({
                title: '',
                content: t,
              });
            }
          })();
        } else {
          dialog.show({
            title: '',
            content: text,
          });
        }
      }
      break;
  }
});

let lng = navigator.language;
const interval = setInterval(() => {
  lng = navigator.language;
  if (lng) {
    remoteChannel.sendServerEvent<languageData>({
      type: remoteType.LANGUAGE,
      data: lng,
    });
    clearInterval(interval);
  }
}, 1000);
